home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / specifier.el.z / specifier.el
Encoding:
Text File  |  1998-05-21  |  16.0 KB  |  418 lines

  1. ;;; specifier.el --- Lisp interface to specifiers
  2.  
  3. ;; Copyright (C) 1995, 1996 Ben Wing.
  4.  
  5. ;; Author: Ben Wing <wing@666.com>
  6. ;; Keywords: internal
  7.  
  8. ;; first appeared in 19.12.
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  24. ;; Free Software Foundation, 59 Temple Place - Suite 330,
  25. ;; Boston, MA 02111-1307, USA.
  26.  
  27. ;;; Synched up with: Not in FSF.
  28.  
  29. (defun make-specifier-and-init (type spec-list &optional dont-canonicalize)
  30.   "Create and initialize a new specifier.
  31.  
  32. This is a front-end onto `make-specifier' that allows you to create a
  33. specifier and add specs to it at the same time.  TYPE specifies the
  34. specifier type.  SPEC-LIST supplies the specification(s) to be added
  35. to the specifier. Normally, almost any reasonable abbreviation of the
  36. full spec-list form is accepted, and is converted to the full form;
  37. however, if optional argument DONT-CANONICALIZE is non-nil, this
  38. conversion is not performed, and the SPEC-LIST must already be in full
  39. form.  See `canonicalize-spec-list'."
  40.   (let ((sp (make-specifier type)))
  41.     (if (not dont-canonicalize)
  42.     (setq spec-list (canonicalize-spec-list spec-list type)))
  43.     (add-spec-list-to-specifier sp spec-list)
  44.     sp))
  45.  
  46. ;; God damn, do I hate dynamic scoping.
  47.  
  48. (defun map-specifier (ms-specifier ms-func &optional ms-locale ms-maparg)
  49.   "Apply MS-FUNC to the specification(s) for MS-LOCALE in MS-SPECIFIER.
  50.  
  51. If MS-LOCALE is a locale, MS-FUNC will be called for that locale.
  52. If MS-LOCALE is a locale type, MS-FUNC will be mapped over all locales
  53. of that type.  If MS-LOCALE is 'all or nil, MS-FUNC will be mapped
  54. over all locales in MS-SPECIFIER.
  55.  
  56. MS-FUNC is called with four arguments: the MS-SPECIFIER, the locale
  57. being mapped over, the inst-list for that locale, and the
  58. optional MS-MAPARG.  If any invocation of MS-FUNC returns non-nil,
  59. the mapping will stop and the returned value becomes the
  60. value returned from `map-specifier'.  Otherwise, `map-specifier'
  61. returns nil."
  62.   (let ((ms-specs (specifier-spec-list ms-specifier ms-locale))
  63.     ms-result)
  64.     (while (and ms-specs (not ms-result))
  65.       (let ((ms-this-spec (car ms-specs)))
  66.     (setq ms-result (funcall ms-func ms-specifier (car ms-this-spec)
  67.                   (cdr ms-this-spec) ms-maparg))
  68.     (setq ms-specs (cdr ms-specs))))
  69.     ms-result))
  70.  
  71. (defun canonicalize-inst-pair (inst-pair specifier-type &optional noerror)
  72.   "Canonicalize the given INST-PAIR.
  73.  
  74. SPECIFIER-TYPE specifies the type of specifier that this SPEC-LIST
  75. will be used for.
  76.  
  77. Canonicalizing means converting to the full form for an inst-pair, i.e.
  78. `(TAG-SET . INSTANTIATOR)'.  A single, untagged instantiator is given
  79. a tag set of nil (the empty set), and a single tag is converted into
  80. a tag set consisting only of that tag.
  81.  
  82. If NOERROR is non-nil, signal an error if the inst-pair is invalid;
  83. otherwise return t."
  84.   ;; OK, the possibilities are:
  85.   ;;
  86.   ;; a) a single instantiator
  87.   ;; b) a cons of a tag and an instantiator
  88.   ;; c) a cons of a tag set and an instantiator
  89.   (cond ((valid-instantiator-p inst-pair specifier-type)
  90.      ;; case (a)
  91.      (cons nil inst-pair))
  92.  
  93.     ((not (consp inst-pair))
  94.      ;; not an inst-pair
  95.      (if noerror t
  96.        ;; this will signal an appropriate error.
  97.        (check-valid-instantiator inst-pair specifier-type)))
  98.  
  99.     ((and (valid-specifier-tag-p (car inst-pair))
  100.           (valid-instantiator-p (cdr inst-pair) specifier-type))
  101.      ;; case (b)
  102.      (cons (list (car inst-pair)) (cdr inst-pair)))
  103.  
  104.     ((and (valid-specifier-tag-set-p (car inst-pair))
  105.           (valid-instantiator-p (cdr inst-pair) specifier-type))
  106.      ;; case (c)
  107.      inst-pair)
  108.      
  109.     (t
  110.      (if noerror t
  111.        (signal 'error (list "Invalid specifier tag set"
  112.                 (car inst-pair)))))))
  113.  
  114. (defun canonicalize-inst-list (inst-list specifier-type &optional noerror)
  115.   "Canonicalize the given INST-LIST (a list of inst-pairs).
  116.  
  117. SPECIFIER-TYPE specifies the type of specifier that this INST-LIST
  118. will be used for.
  119.  
  120. Canonicalizing means converting to the full form for an inst-list, i.e.
  121. `((TAG-SET . INSTANTIATOR) ...)'.  This function accepts a single
  122. inst-pair or any abbreviation thereof or a list of (possibly
  123. abbreviated) inst-pairs. (See `canonicalize-inst-pair'.)
  124.  
  125. If NOERROR is non-nil, signal an error if the inst-list is invalid;
  126. otherwise return t."
  127.  
  128.   ;; OK, the possibilities are:
  129.   ;;
  130.   ;; a) an inst-pair or various abbreviations thereof
  131.   ;; b) a list of (a)
  132.   (let ((result (canonicalize-inst-pair inst-list specifier-type t)))
  133.     (if (not (eq result t))
  134.     ;; case (a)
  135.     (list result)
  136.  
  137.       (if (not (consp inst-list))
  138.       ;; not an inst-list.
  139.       (if noerror t
  140.        ;; this will signal an appropriate error.
  141.         (check-valid-instantiator inst-list specifier-type))
  142.  
  143.     ;; case (b)
  144.     (catch 'cann-inst-list
  145.       ;; don't use mapcar here; we need to catch the case of
  146.       ;; an invalid list.
  147.       (let ((rest inst-list)
  148.         (result nil))
  149.         (while rest
  150.           (if (not (consp rest))
  151.           (if noerror (throw 'cann-inst-list t)
  152.             (signal 'error (list "Invalid list format" inst-list)))
  153.         (let ((res2 (canonicalize-inst-pair (car rest) specifier-type
  154.                             noerror)))
  155.           (if (eq res2 t)
  156.               ;; at this point, we know we're noerror because
  157.               ;; otherwise canonicalize-inst-pair would have
  158.               ;; signalled an error.
  159.               (throw 'cann-inst-list t)
  160.             (setq result (cons res2 result)))))
  161.           (setq rest (cdr rest)))
  162.         (nreverse result)))))))
  163.  
  164. (defun canonicalize-spec (spec specifier-type &optional noerror)
  165.   "Canonicalize the given SPEC (a specification).
  166.  
  167. SPECIFIER-TYPE specifies the type of specifier that this SPEC-LIST
  168. will be used for.
  169.  
  170. Canonicalizing means converting to the full form for a spec, i.e.
  171. `(LOCALE (TAG-SET . INSTANTIATOR) ...)'.  This function accepts a
  172. possibly abbreviated inst-list or a cons of a locale and a possibly
  173. abbreviated inst-list. (See `canonicalize-inst-list'.)
  174.  
  175. If NOERROR is nil, signal an error if the specification is invalid;
  176. otherwise return t."
  177.   ;; OK, the possibilities are:
  178.   ;;
  179.   ;; a) an inst-list or some abbreviation thereof
  180.   ;; b) a cons of a locale and an inst-list
  181.   (let ((result (canonicalize-inst-list spec specifier-type t)))
  182.     (if (not (eq result t))
  183.     ;; case (a)
  184.     (cons 'global result)
  185.  
  186.       (if (not (consp spec))
  187.       ;; not a spec.
  188.       (if noerror t
  189.         ;; this will signal an appropriate error.
  190.         (check-valid-instantiator spec specifier-type))
  191.  
  192.     (if (not (valid-specifier-locale-p (car spec)))
  193.         ;; invalid locale.
  194.         (if noerror t
  195.           (signal 'error (list "Invalid specifier locale" (car spec))))
  196.  
  197.       ;; case (b)
  198.       (let ((result (canonicalize-inst-list (cdr spec) specifier-type
  199.                         noerror)))
  200.         (if (eq result t)
  201.         ;; at this point, we know we're noerror because
  202.         ;; otherwise canonicalize-inst-list would have
  203.         ;; signalled an error.
  204.         t
  205.           (cons (car spec) result))))))))
  206.  
  207. (defun canonicalize-spec-list (spec-list specifier-type &optional noerror)
  208.   "Canonicalize the given SPEC-LIST (a list of specifications).
  209.  
  210. SPECIFIER-TYPE specifies the type of specifier that this SPEC-LIST
  211. will be used for.
  212.  
  213. Canonicalizing means converting to the full form for a spec-list, i.e.
  214. `((LOCALE (TAG-SET . INSTANTIATOR) ...) ...)'.  This function accepts
  215. a possibly abbreviated specification or a list of such things. (See
  216. `canonicalize-spec'.) This is the function used to convert spec-lists
  217. accepted by `set-specifier' and such into a form suitable for
  218. `add-spec-list-to-specifier'.
  219.  
  220. This function tries extremely hard to resolve any ambiguities,
  221. and the built-in specifier types (font, image, toolbar, etc.) are
  222. designed so that there won't be any ambiguities.
  223.  
  224. If NOERROR is nil, signal an error if the spec-list is invalid;
  225. otherwise return t."
  226.   ;; OK, the possibilities are:
  227.   ;;
  228.   ;; a) a spec or various abbreviations thereof
  229.   ;; b) a list of (a)
  230.   (let ((result (canonicalize-spec spec-list specifier-type t)))
  231.     (if (not (eq result t))
  232.     ;; case (a)
  233.     (list result)
  234.  
  235.       (if (not (consp spec-list))
  236.       ;; not a spec-list.
  237.       (if noerror t
  238.        ;; this will signal an appropriate error.
  239.         (check-valid-instantiator spec-list specifier-type))
  240.  
  241.     ;; case (b)
  242.     (catch 'cann-spec-list
  243.       ;; don't use mapcar here; we need to catch the case of
  244.       ;; an invalid list.
  245.       (let ((rest spec-list)
  246.         (result nil))
  247.         (while rest
  248.           (if (not (consp rest))
  249.           (if noerror (throw 'cann-spec-list t)
  250.             (signal 'error (list "Invalid list format" spec-list)))
  251.         (let ((res2 (canonicalize-spec (car rest) specifier-type
  252.                            noerror)))
  253.           (if (eq res2 t)
  254.               ;; at this point, we know we're noerror because
  255.               ;; otherwise canonicalize-spec would have
  256.               ;; signalled an error.
  257.               (throw 'cann-spec-list t)
  258.             (setq result (cons res2 result)))))
  259.           (setq rest (cdr rest)))
  260.         (nreverse result)))))))
  261.  
  262. (defun set-specifier (specifier value &optional locale tag-set how-to-add)
  263.   "Add a specification or specifications to SPECIFIER.
  264.  
  265. This function adds a specification of VALUE in locale LOCALE.
  266. LOCALE indicates where this specification is active, and should be
  267. a buffer, a window, a frame, a device, or the symbol `global' to
  268. indicate that it applies everywhere.  LOCALE usually defaults to
  269. `global' if omitted.
  270.  
  271. VALUE is usually what is called an \"instantiator\" (which, roughly
  272. speaking, corresponds to the \"value\" of the property governed by
  273. SPECIFIER).  The valid instantiators for SPECIFIER depend on the
  274. type of SPECIFIER (which you can determine using `specifier-type').
  275. The specifier `scrollbar-width', for example, is of type `integer',
  276. meaning its valid instantiators are integers.  The specifier
  277. governing the background color of the `default' face (you can
  278. retrieve this specifier using `(face-background 'default)') is
  279. of type `color', meaning its valid instantiators are strings naming
  280. colors and color-instance objects.  For some types of specifiers,
  281. such as `image' and `toolbar', the instantiators can be very
  282. complex.  Generally this is documented in the appropriate predicate
  283. function -- `color-specifier-p', `image-specifier-p',
  284. `toolbar-specifier-p', etc.
  285.  
  286. NOTE: It does *not* work to give a VALUE of nil as a way of
  287. removing the specifications for a locale.  Use `remove-specifier'
  288. instead. (And keep in mind that, if you omit the LOCALE argument
  289. to `remove-specifier', it removes *all* specifications!  If you
  290. want to remove just the `global' specification, make sure to
  291. specify a LOCALE of `global'.)
  292.  
  293. VALUE can also be a list of instantiators.  This means basically,
  294. \"try each one in turn until you get one that works\".  This allows
  295. you to give funky instantiators that may only work in some cases,
  296. and provide more normal backups for the other cases. (For example,
  297. you might like the color \"darkseagreen2\", but some X servers
  298. don't recognize this color, so you could provide a backup
  299. \"forest green\".  Color TTY devices probably won't recognize this
  300. either, so you could provide a second backup \"green\".  You'd
  301. do this by specifying this list of instantiators:
  302.  
  303. '(\"darkseagreen2\" \"forest green\" \"green\")
  304.  
  305. VALUE can also be various more complicated forms; see below.
  306.  
  307. Optional argument TAG-SET is a tag or a list of tags, to be associated
  308. with the VALUE.  Tags are symbols (usually naming device types, such
  309. as `x' and `tty', or device classes, such as `color', `mono', and
  310. `grayscale'); specifying a TAG-SET restricts the scope of VALUE to
  311. devices that match all specified tags. (You can also create your
  312. own tags using `define-specifier-tag', and use them to identify
  313. specifications added by you, so you can remove them later.)
  314.  
  315. Optional argument HOW-TO-ADD should be either nil or one of the
  316. symbols `prepend', `append', `remove-tag-set-prepend',
  317. `remove-tag-set-append', `remove-locale', `remove-locale-type',
  318. or `remove-all'.  This specifies what to do with existing
  319. specifications in LOCALE (and possibly elsewhere in the specifier).
  320. Most of the time, you do not need to worry about this argument;
  321. the default behavior of `remove-tag-set-prepend' is usually fine.
  322. See `copy-specifier' and `add-spec-to-specifier' for a full
  323. description of what each of these means.
  324.  
  325. VALUE can actually be anything acceptable to `canonicalize-spec-list';
  326. this includes, among other things:
  327.  
  328. -- a cons of a locale and an instantiator (or list of instantiators)
  329. -- a cons of a tag or tag-set and an instantiator (or list of
  330.    instantiators)
  331. -- a cons of a locale and the previous type of item
  332. -- a list of one or more of any of the previous types of items
  333.  
  334. However, in these cases, you cannot give a LOCALE or TAG-SET,
  335. because they do not make sense. (You will probably get an error if
  336. you try this.)
  337.  
  338. Finally, VALUE can itself be a specifier (of the same type as
  339. SPECIFIER), if you want to copy specifications from one specifier
  340. to another; this is equivalent to calling `copy-specifier', and
  341. LOCALE, TAG-SET, and HOW-TO-ADD have the same semantics as with
  342. that function.
  343.  
  344. Note that `set-specifier' is exactly complementary to `specifier-specs'
  345. except in the case where SPECIFIER has no specs at all in it but nil
  346. is a valid instantiator (in that case, `specifier-specs' will return
  347. nil (meaning no specs) and `set-specifier' will interpret the `nil'
  348. as meaning \"I'm adding a global instantiator and its value is `nil'\"),
  349. or in strange cases where there is an ambiguity between a spec-list
  350. and an inst-list, etc. (The built-in specifier types are designed
  351. in such a way as to avoid any such ambiguities.)
  352.  
  353. NOTE: If you want to work with spec-lists, you should probably not
  354. use either `set-specifier' or `specifier-specs', but should use the
  355. lower-level functions `add-spec-list-to-specifier' and `specifier-spec-list'.
  356. These functions always work with fully-qualified spec-lists; thus, there
  357. is no possibility for ambiguity and no need to go through the function
  358. `canonicalize-spec-list', which is potentially time-consuming."
  359.  
  360.   ;; backward compatibility: the old function had HOW-TO-ADD as the
  361.   ;; third argument and no arguments after that.
  362.   ;; #### this should disappear at some point.
  363.   (if (and (null how-to-add)
  364.        (memq locale '(prepend append remove-tag-set-prepend
  365.                   remove-tag-set-append remove-locale
  366.                   remove-locale-type remove-all)))
  367.       (progn
  368.     (setq how-to-add locale)
  369.     (setq locale nil)))
  370.  
  371.   ;; proper beginning of the function.
  372.   (let ((is-valid (valid-instantiator-p value (specifier-type specifier)))
  373.     (nval value))
  374.     (cond ((and (not is-valid) (specifierp nval))
  375.        (copy-specifier nval specifier locale tag-set nil how-to-add))
  376.       (t
  377.        (if tag-set
  378.            (progn
  379.          (if (not (listp tag-set))
  380.              (setq tag-set (list tag-set)))
  381.          ;; You tend to get more accurate errors
  382.          ;; for a variety of cases if you call
  383.          ;; canonicalize-tag-set here.
  384.          (setq tag-set (canonicalize-tag-set tag-set))
  385.          (if (and (not is-valid) (consp nval))
  386.              (setq nval
  387.                (mapcar #'(lambda (x)
  388.                        (check-valid-instantiator
  389.                     x (specifier-type specifier))
  390.                        (cons tag-set x))
  391.                    nval))
  392.            (setq nval (cons tag-set nval)))))
  393.        (if locale
  394.            (setq nval (cons locale nval)))
  395.        (add-spec-list-to-specifier
  396.         specifier
  397.         (canonicalize-spec-list nval (specifier-type specifier))
  398.         how-to-add))))
  399.   value)
  400.  
  401. (define-specifier-tag 'win 'device-on-window-system-p)
  402.  
  403. ;; Add tags for device types that don't have support compiled
  404. ;; into the binary that we're about to dump.  This will prevent
  405. ;; code like
  406. ;;
  407. ;; (set-face-foreground 'default "black" nil '(x color))
  408. ;;
  409. ;; from producing an error if no X support was compiled in.
  410.  
  411. (or (valid-specifier-tag-p 'x)
  412.     (define-specifier-tag 'x (lambda (dev) (eq (device-type dev) 'x))))
  413. (or (valid-specifier-tag-p 'tty)
  414.     (define-specifier-tag 'tty (lambda (dev) (eq (device-type dev) 'tty))))
  415. (or (valid-specifier-tag-p 'mswindows)
  416.     (define-specifier-tag 'mswindows (lambda (dev)
  417.                        (eq (device-type dev) 'mswindows))))
  418.